/**
 * Created by IntelliJ
 * Author:熊磊
 */
//只有头结点的双向列表
public class DoubleLinkedList {
    HeroNode2 headNode=new HeroNode2(0,"","");


    public static void main(String[] args) {
        DoubleLinkedList doubleLinkedList = new DoubleLinkedList();
        HeroNode2 heroNode1 = new HeroNode2(1, "宋江", "及时雨");
        HeroNode2 heroNode2 = new HeroNode2(2, "卢俊义", "玉麒麟");
        HeroNode2 heroNode3 = new HeroNode2(3, "吴用", "智多星");
        HeroNode2 heroNode4 = new HeroNode2(4, "林冲", "豹子头");
        HeroNode2 heroNode5 = new HeroNode2(5, "鲁智深", "花和尚");
        doubleLinkedList.add(heroNode1);
        doubleLinkedList.add(heroNode2);
        doubleLinkedList.add(heroNode3);
        doubleLinkedList.add(heroNode4);
        doubleLinkedList.add(heroNode5);
        doubleLinkedList.del(1);
        doubleLinkedList.del(5);
        doubleLinkedList.del(3);
        doubleLinkedList.list();
        heroNode4 = new HeroNode2(4, "啊林", "豹子头");
        doubleLinkedList.update(heroNode4);
        doubleLinkedList.list();
        //System.out.println(doubleLinkedList.headNode.next.next.pre);
        //System.out.println("============");
        //System.out.println(doubleLinkedList.headNode);
        //System.out.println(doubleLinkedList.headNode.next);
        //System.out.println(doubleLinkedList.headNode.next.next);
        //System.out.println(doubleLinkedList.headNode.next.next.pre);
        //System.out.println(doubleLinkedList.headNode.next.next.pre.pre);
    }

    //无序添加
    public void add(HeroNode2 heroNode){
        HeroNode2 temp = headNode;
        while (true){
            if (temp.next==null) {break;}
            temp=temp.next;
        }
        temp.next=heroNode;
        heroNode.pre=temp;
    }


    //有序添加 TODO


    //修改
    public void update(HeroNode2 newHeroNode){
        HeroNode2 temp = headNode;
        if (temp.next==null){
            System.out.println("链表为空~");
            return;
        }
        while (true){
            temp=temp.next;
            if (temp.no==newHeroNode.no){
                temp.name=newHeroNode.name;
                temp.nickName=newHeroNode.nickName;
                break;
            }
            if (temp==null){
                System.out.println("未找到编号为"+newHeroNode.no+"要修改的结点");
                break;
            }
        }
    }

    //删除结点
    public void del(int no){
        HeroNode2 temp = headNode;
        while (true){
            temp=temp.next;
            if (temp==null){
                System.out.println("未找到对应编号的结点");
                break;
            }
            if (temp.no==no){
                temp.pre.next=temp.next;
                if (temp.next!=null){temp.next.pre=temp.pre;}
                break;
            }
        }
    }

    //遍历链表并输出
    public void list(){
        if (headNode.next==null){
            System.out.println("链表为空");
            return;
        }
        HeroNode2 temp = headNode.next;
        while (true){
            System.out.println(temp);
            if (temp.next==null){
                break;
            }
            temp= temp.next;
        }
    }
}
class HeroNode2{
    public int no;
    public String name;
    public String nickName;
    public HeroNode2 next;
    public HeroNode2 pre;

    public HeroNode2(int no, String name, String nickName) {
        this.no = no;
        this.name = name;
        this.nickName = nickName;
    }

    @Override
    public String toString() {
        return "Hero{ " +no +"—"+nickName+name+" }";
    }
}

